package com.zzb.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.zzb.DTO.ResultDTO;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter("/*")
public class MyFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        String path = req.getServletPath();
        if (path.startsWith("/back/")) {
            String username = (String) req.getSession().getAttribute("username");
            if (username == null) {
                res.getWriter().println(JSONObject.toJSONString(ResultDTO.getResult("未登录")));
            } else {
// 放行
                chain.doFilter(req, res);
            }
        } else {
            chain.doFilter(req, res);
        }

    }
}